/**
 Ceated by Amit Mudgal (MicroStrategy Principal Designer) for GMS on 6/20/2014
 *
 */

package com.amex.uam.tasks;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.amex.SQL.DBEditProfileServiceHandler;
import com.amex.SQL.DBServiceHandler;
import com.amex.SQL.Utils;
import com.amex.uam.tasks.MailSender.RPA;
import com.microstrategy.web.beans.MarkupOutput;
import com.microstrategy.web.beans.RequestKeys;
import com.microstrategy.web.tasks.AbstractBaseTask;
import com.microstrategy.web.tasks.TaskParameterMetadata;
import com.microstrategy.web.tasks.TaskRequestContext;
import com.microstrategy.web.tasks.Task;

public class GetNZData_bak extends AbstractBaseTask implements Task  {
	private TaskParameterMetadata taskSubID;
	private TaskParameterMetadata param1;
	
	public GetNZData_bak() {
		super("Gets data from NZ");
		taskSubID = addParameterMetadata("TaskSubID", "Enter Operation", true, "");
		param1 = addParameterMetadata("param1", "Parameter for task if needed", false, "");
	}

    public void processRequest(TaskRequestContext context, MarkupOutput out) {

		RequestKeys keys = context.getRequestKeys();
		String taskSub = keys.getValue(taskSubID.getName());
		String Param1 = keys.getValue(param1.getName());
    	
		if (taskSub.equalsIgnoreCase("FunctionalGroup")) {
        	//System.out.println("Called task successfully" + Param1 + taskSub);
        	String FunctionalGroup = DBServiceHandler.getFunctionalGroups();
        	out.append(FunctionalGroup);
		} else if (taskSub.equalsIgnoreCase("FunctionalRole")) {
			// System.out.println("Called task successfully" + Param1 + taskSub);
			String FunctionalRole = DBServiceHandler.getFunctionalRole();
			out.append(FunctionalRole);
		} else if (taskSub.equalsIgnoreCase("FormDataSubmit")) {
        	
        	UserProfile up = new UserProfile();
        	up.setUSER_ACSS_TYPE_CD(keys.getValue("preMstrAType"));
        	up.setWORKFORCE_MBR_ID(keys.getValue("preamexWebID"));
        	up.setUSER_1ST_NM(keys.getValue("disabledPreFN"));
        	up.setUSER_MI_TX(keys.getValue("disabledPreMN"));
        	up.setUSER_LST_NM(keys.getValue("disabledPreLN"));
        	up.setUSER_ID(keys.getValue("preempContID"));
        	up.setDEPT_ID(keys.getValue("predepartmentId"));
        	up.setAMEX_BUS_CD(keys.getValue("preBusnUC"));
        	up.setAMEX_UNIT_CD(keys.getValue("preAmexUC"));
        	up.setCOST_CTR_CD(keys.getValue("preCostCenter"));
        	up.setFUNC_GRP_ROLE_ID(keys.getValueCount("funtcRoleStep2"));
        	up.setUSER_CMPL_PHONE_NO(keys.getValue("prephone"));
        	up.setUSER_EMAIL_AD_TX(keys.getValue("preemail"));
        	up.setWORKFORCE_MBR_MAN_1ST_NM(keys.getValue("prefirstname"));
        	up.setWORKFORCE_MBR_MAN_MI_TX(keys.getValue("premIn"));
        	up.setWORKFORCE_MBR_MAN_LST_NM(keys.getValue("prelastname"));
        	up.setWORKFORCE_MBR_MAN_EMAIL_AD_TX(keys.getValue("preMEmail"));
        	up.setVP_NM(keys.getValue("preVPName"));
        	up.setACSS_RSN_TX(keys.getValue("prereason"));
        	up.setUSER_ADMIN_STA_CD("0");
        	up.setUSER_STA_CD("0");
        	up.setUSER_GRP_ID(0);
        	up.setUSER_TYPE_CD("0");
        	up.setLST_LOGIN_TS(new Timestamp(System.currentTimeMillis()));
        	up.setLST_UPDT_TS(new Timestamp(System.currentTimeMillis()));
        	up.setCREAT_TS(new Timestamp(System.currentTimeMillis()));
        	up.setGEO_ACSS_LVL_CD("G");
        	up.setLIF_GLBL_ACS_IN("N");
        	up.setWORKFORCE_MBR_CLASS_CD("C");
        	up.setCLM_GLBL_ACS_IN("Y");
        	up.setUSER_ACSS_REJCT_RSN_TX("");
        	up.setSEC_XCPT_END_DT(new Timestamp(System.currentTimeMillis()));
        	up.setNDA_TS(new Timestamp(System.currentTimeMillis()));
        	
        	boolean success = DBServiceHandler.WritetoDBPreApprovedRegistration(up);
        	//boolean success = true;
        	if (success) {
        		/*<option value="1" >  Dashboard	</Option>
		 		  <option value="2" >  MARC			</Option>
				  <option value="3" >  ICARD		</Option>  */
        		String product = keys.getValue("destList");
        		System.out.println("product: " + product);
        		String managerMail = up.getWORKFORCE_MBR_MAN_EMAIL_AD_TX();
        		String userMail = up.getUSER_EMAIL_AD_TX();
        		
				if (product != null && managerMail != null && userMail != null) {
        			
					RPA rpa = parseRPA(product);
	        			
	        		String subject = "test subject";
	        		String content = "test content";
	        		
					MailBean bean = MailSender.createRPAMailBean(rpa, managerMail, userMail, subject, content);
	        		
					/*open following code when get email host name*/
					//MailSender.send(bean);
        		}
        		
        	}
		} else if (taskSub.equalsIgnoreCase("GetUserProfile")) {
			String userID = keys.getValue("userid");
			System.out.println("testing speed");
			String data = getUserProfileToBeEdited(userID);
			System.out.println("data::"+data);
			String responseContent = null;
			if (data != null ) {
				StringBuffer sb= new StringBuffer("{");
				sb.append("'status':").append(UserProfileServiceConstant.RESPONSE_OK).append(",");
				sb.append("'data':").append(data).append("}");
				responseContent = sb.toString();	
			} else {
				responseContent = new UserProfileMessage(UserProfileServiceConstant.RESPONSE_ERROR).toJSON();
			}
			System.out.println("ttttt");
			System.out.println("responseContent"+responseContent);
			out.append(responseContent);
        }
        
    }
    
    public static String getUserProfileToBeEdited(String userID) {
    	
    	Map userProfile = DBEditProfileServiceHandler.getUserProfile(userID);
    	System.out.println("userID::"+userID);
		String amexWebId = (String) userProfile.get("amexWebId");
		List<Map<String, Object>> optBlueList = DBEditProfileServiceHandler.getAccessReasonList();
		// DBEditProfileServiceHandler.deleteUserOptBlue(amexWebId);
		// DBEditProfileServiceHandler.insertUserOptBlue(params);
		List<Map<String, String>> functionalGroups = DBEditProfileServiceHandler.getFunctionalGroups();
		List<String> secGroups = DBEditProfileServiceHandler.getSecGrp(amexWebId);
		List<String> userSecGroups = DBEditProfileServiceHandler.getUserSecGrp(amexWebId);
		List<String> geoAcssLvlCdList = DBEditProfileServiceHandler.getGeoAcssLvlCd(amexWebId);
		List<String> userRgnAcssList = DBEditProfileServiceHandler.getUserRgnAcss(amexWebId);
		//List<String> ctryNameList = DBEditProfileServiceHandler.getCtryNm(amexWebId);
    	List<String> ctryNameList = new ArrayList<String>();
    	List<Map<String, String>> funcGRPRole = DBEditProfileServiceHandler.getFuncGRPRole();
		
		StringBuffer sb = new StringBuffer("{");
		sb.append("userProfile:").append(Utils.toJSON(userProfile)).append(",");
		sb.append("optBlueList:").append(Utils.toJSON(optBlueList)).append(",");
		sb.append("functionalGroups:").append(Utils.toJSON(functionalGroups)).append(",");
		sb.append("secGroups:").append(Utils.toJSON(secGroups)).append(",");
		sb.append("userSecGroups:").append(Utils.toJSON(userSecGroups)).append(",");
		sb.append("geoAcssLvlCdList:").append(Utils.toJSON(geoAcssLvlCdList)).append(",");
		sb.append("userRgnAcssList:").append(Utils.toJSON(userRgnAcssList)).append(",");
		sb.append("ctryNameList:").append(Utils.toJSON(ctryNameList)).append(",");
		sb.append("funcGRPRole:").append(Utils.toJSON(funcGRPRole));
		sb.append("}");
		
		
		
		return sb.toString();
    	
    }
    
    private MailSender.RPA parseRPA(String product) {
		if (product == null)
			return null;
		
		RPA rpa = null;
		product = product.toUpperCase();
		int sum = 0;
		if (product.indexOf("MARC") != -1) {
			sum += 1;
		}
		if (product.indexOf("DASHBOARD") != -1) {
			sum += 2;
		}
		if (product.indexOf("ICARD") != -1) {
			sum += 4;
		}

		switch (sum) {
		case 1:
			rpa = MailSender.RPA.MARC;
			break;
		case 2:
			rpa = MailSender.RPA.DASHBOARD;
			break;
		case 4:
			rpa = MailSender.RPA.ICARD;
			break;
		default:
			rpa = MailSender.RPA.ALL;
		}
		return rpa;
	}
    
/*    public static void main(String args[]){
    	System.out.println("fff");
    	System.out.println(getUserProfileToBeEdited("8165840"));
    }*/
}